package com.ceba.test.modules.email.entity;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ceba.test.base.enums.EmailTypeEnum;
import com.ceba.test.base.enums.PeopleTypeEnum;

public class EmailEntity {
    private String hostName;//smtp服务器名 【必填项】
    private String account;//授权账号 【必填项】
    private String password;//授权密码 【必填项】
    private String content;//邮件内容   文本内容 或 HTML内容 【必填项】
    private String subject;//邮件主题 【必填项】
    private EmailTypeEnum emailTypeEnum;//发送邮件类型 【必填项】默认IMAGE_HTML_EMAIL
	private List<EmailRelationPeople> peopleList;//【必填项】
	
	private String charset;//编码 默认编码UTF-8
	private List<EmailRelationFile> fileList;
	
	
	public EmailEntity(){
		this.emailTypeEnum=EmailTypeEnum.IMAGE_HTML_EMAIL;
		this.charset="UTF-8";//编码 默认编码UTF-8
	}
	
	public EmailEntity(String hostName,String account,String password,String content,String subject,EmailTypeEnum emailTypeEnum,List<EmailRelationPeople> peopleList){
		this();
		if(emailTypeEnum == null){
			this.emailTypeEnum=EmailTypeEnum.IMAGE_HTML_EMAIL;
		}else{
			this.emailTypeEnum=emailTypeEnum;
		}
		this.hostName=hostName;
		this.account=account;
		this.password=password;
		this.content=content;
		this.subject=subject;
		this.peopleList=peopleList;
	}
	
	
	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getHostName() {
		return hostName;
	}


	public void setHostName(String hostName) {
		this.hostName = hostName;
	}


	public String getAccount() {
		return account;
	}


	public void setAccount(String account) {
		this.account = account;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}


	public String getSubject() {
		return subject;
	}


	public void setSubject(String subject) {
		this.subject = subject;
	}


	public EmailTypeEnum getEmailTypeEnum() {
		return emailTypeEnum;
	}


	public void setEmailTypeEnum(EmailTypeEnum emailTypeEnum) {
		this.emailTypeEnum = emailTypeEnum;
	}


	public List<EmailRelationPeople> getPeopleList() {
		return peopleList;
	}


	public void setPeopleList(List<EmailRelationPeople> peopleList) {
		this.peopleList = peopleList;
	}


	public List<EmailRelationFile> getFileList() {
		return fileList;
	}


	public void setFileList(List<EmailRelationFile> fileList) {
		this.fileList = fileList;
	}

	
	@Override
	public String toString() {
		return "EmailEntity [hostName=" + hostName + ", account=" + account + ", password=" + password + ", content="
				+ content + ", subject=" + subject + ", emailTypeEnum=" + emailTypeEnum + ", peopleList=" + peopleList
				+ ", fileList=" + fileList + "]";
	}
	
	
	public void valid() throws Exception{
		if(StringUtils.isEmpty(hostName)){
			throw new NullPointerException("hostName:smtp服务器名不能为空");
		}
		
		if(StringUtils.isEmpty(account)){
			throw new NullPointerException("account:授权账号");
		}
		
		if(StringUtils.isEmpty(password)){
			throw new NullPointerException("password:密码不能为空");
		}
		
		if(StringUtils.isEmpty(content)){
			throw new NullPointerException("content:收邮件内容不能为空");
		}
		
		if(StringUtils.isEmpty(subject)){
			throw new NullPointerException("subject:邮件主题不能为空");
		}
		
		if(peopleList==null || peopleList.size()==0){
			throw new NullPointerException("peopleList:邮件相关的人不能为空");
		}
		
		boolean sendBool=false,receiveBool=false;
		
		for(EmailRelationPeople emailRelationPeople:peopleList){
			emailRelationPeople.valid();
			if(emailRelationPeople.getPeopleTypeEnum()==PeopleTypeEnum.RECIPIENTS){
				sendBool=true;
			}else if(emailRelationPeople.getPeopleTypeEnum()==PeopleTypeEnum.ADDTO){
				receiveBool=true;
			}
		}
		
		if(!sendBool){
			throw new Exception("peopleList:不能没有发件人");
		}
		if(!receiveBool){
			throw new Exception("peopleList:不能没有收件人");
		}
		
		if(fileList !=null && fileList.size() != 0){
			for(EmailRelationFile file:fileList){
				file.valid();
			}
		}
		
	}
}
